package com.superteam.zzl.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.superteam.zzl.activity.SearchMoreActivity.DynMoreActivity;
import com.superteam.zzl.activity.SearchMoreActivity.PoiMoreActivity;
import com.superteam.zzl.activity.SearchMoreActivity.ShopMoreActivity;
import com.superteam.zzl.activity.SearchMoreActivity.UserMoreActivity;
import com.superteam.zzl.activity.databinding.ActivitySearchResultBinding;
import com.superteam.zzl.adapter.SearchAdapter.SearchResultAdapter;
import com.superteam.zzl.app.MyApplication;
import com.superteam.zzl.model.Search.SearchData;
import com.superteam.zzl.retrofit.RecommendFragment.MainRetrofit;
import com.superteam.zzl.utils.ListViewUtils;
import com.superteam.zzl.viewmodel.Search.Data;
import com.superteam.zzl.viewmodel.Search.RequestData;
import com.superteam.zzl.widget.RecommandListView;

import java.util.ArrayList;

import kotlin.jvm.Synchronized;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class SearchResultActivity extends BaseActivity {

    private ActivitySearchResultBinding resultBinding;
    private RecommandListView resultLv, dynLv, shopLv, traLv, userLv, gropLv;
    private RequestData requestData;
    private SearchResultAdapter adapter;
    private ArrayList<Object> list;
    public static Handler handler;
    private Data data;
    private ProgressBar pb;
    private TextView back;
    private ScrollView sc;
    private EditText et;
    public static RelativeLayout rl_1, rl_2, rl_3, rl_4, rl_5, rl_6;
    private TextView tv_more_1;

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public ViewDataBinding initDataBinding() {
        resultBinding = DataBindingUtil.setContentView(this, R.layout.activity_search_result);
        init();
        loadData();
        return resultBinding;
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {

    }

    private void init() {
        resultLv = resultBinding.resLvResult;
        pb = resultBinding.pbResult;
        sc = resultBinding.resultSc;
        rl_1 = resultBinding.rl1;
        rl_2 = resultBinding.rl2;
        rl_3 = resultBinding.rl3;
        rl_4 = resultBinding.rl4;
        rl_5 = resultBinding.rl5;
        rl_6 = resultBinding.rl6;
        tv_more_1 = resultBinding.tvDynMore;
        dynLv = resultBinding.lvResultDes;
        shopLv = resultBinding.lvResultShop;
        traLv = resultBinding.lvResultTra;
        userLv = resultBinding.lvResultUser;
        gropLv = resultBinding.lvResultGrop;
        back = (TextView) findViewById(R.id.in_result).findViewById(R.id.tv_search_1);
        et = (EditText) findViewById(R.id.in_result).findViewById(R.id.et_search);
        et.setText(MyApplication.search);

        list = new ArrayList<>();
        gropLv.setEmptyView(pb);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(900,intent);
                finish();
            }
        });

        setOnClick();


    }

    private void setOnClick() {
        resultBinding.setClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_Dyn_more://更多动态
                        startActivityForResult(new Intent(SearchResultActivity.this, DynMoreActivity.class), 678);
                        break;
                    case R.id.tv_Shop_more://更多商品
                        startActivityForResult(new Intent(SearchResultActivity.this, ShopMoreActivity.class),666);
                        break;
                    case R.id.tv_Tra_more://更多旅行地
                        startActivityForResult(new Intent(SearchResultActivity.this, PoiMoreActivity.class),777);
                        break;
                    case R.id.tv_User_more://更多用户
                        startActivityForResult(new Intent(SearchResultActivity.this, UserMoreActivity.class),888);
                        break;
                    case R.id.tv_Grop_more://更多群组
                        //startActivityForResult(new Intent(SearchResultActivity.this));
                        break;
                }
            }
        });
    }


    private void loadData() {
        data = new Data();
        requestData = new RequestData(data);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                //结果
                if (msg.what == 0x222) {
                    rl_1.setVisibility(View.VISIBLE);
                    list = new ArrayList<>();
                    for (int i = 0; i < data.placeList.size(); i++) {
                        list.add(data.placeList.get(i));
                    }
                    adapter = new SearchResultAdapter(SearchResultActivity.this, list);
                    resultLv.setAdapter(adapter);
                    ListViewUtils.setListViewHeightBasedOnChildren(resultLv);

                }
                //动态
                if (msg.what == 0x223) {
                    rl_2.setVisibility(View.VISIBLE);
                    if (data.trendList.size() > 0) {
                        sc.setVisibility(View.VISIBLE);
                    }
                    list = new ArrayList<>();
                    for (int i = 0; i < data.trendList.size(); i++) {
                        list.add(data.trendList.get(i));
                    }
                    adapter = new SearchResultAdapter(SearchResultActivity.this, list);
                    dynLv.setAdapter(adapter);
                    ListViewUtils.setListViewHeightBasedOnChildren(dynLv);

                }
                //商品
                if (msg.what == 0x224) {

                    rl_3.setVisibility(View.VISIBLE);
                    list = new ArrayList<>();
                    for (int i = 0; i < data.productList.size(); i++) {
                        list.add(data.productList.get(i));
                    }
                    adapter = new SearchResultAdapter(SearchResultActivity.this, list);
                    shopLv.setAdapter(adapter);
                    ListViewUtils.setListViewHeightBasedOnChildren(shopLv);

                }
                //旅行地
                if (msg.what == 0x225) {

                    rl_4.setVisibility(View.VISIBLE);
                    list = new ArrayList<>();
                    for (int i = 0; i < data.poiList.size(); i++) {
                        list.add(data.poiList.get(i));
                    }
                    adapter = new SearchResultAdapter(SearchResultActivity.this, list);
                    traLv.setAdapter(adapter);
                    ListViewUtils.setListViewHeightBasedOnChildren(traLv);

                }
                //用户
                if (msg.what == 0x226) {

                    rl_5.setVisibility(View.VISIBLE);
                    list = new ArrayList<>();
                    for (int i = 0; i < data.userList.size(); i++) {
                        list.add(data.userList.get(i));
                    }
                    adapter = new SearchResultAdapter(SearchResultActivity.this, list);
                    userLv.setAdapter(adapter);
                    ListViewUtils.setListViewHeightBasedOnChildren(userLv);

                }
                //群组
                if (msg.what == 0x227) {

                    rl_6.setVisibility(View.VISIBLE);
                    list = new ArrayList<>();
                    for (int i = 0; i < data.groupList.size(); i++) {
                        list.add(data.groupList.get(i));
                    }
                    adapter = new SearchResultAdapter(SearchResultActivity.this, list);
                    gropLv.setAdapter(adapter);
                    ListViewUtils.setListViewHeightBasedOnChildren(gropLv);

                }

                return true;
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Util.isOnMainThread()) {
            Glide.get(getApplicationContext()).clearMemory();
        }

    }
}
